<template>
  <section>
    <van-sticky>
      <PageHeader title="修改密码"/>
    </van-sticky>

    <van-form @submit="onFormSubmit">
      <van-field
        v-model="formData.password"
        name="password"
        label="密码"
        placeholder="请输入密码"
        type="password"
        :rules="[{ required: true, message: '请输入密码' }]"
      />

      <van-field
        v-model="formData.repeat_password"
        name="repeat_password"
        label="再次输入密码"
        placeholder="请输入密码"
        type="password"
        :rules="[{ required: true, message: '请输入密码' }]"
      />

      <div style="margin: 16px;">
        <van-button round block type="info" native-type="submit">
          保存
        </van-button>
      </div>
    </van-form>

  </section>
</template>

<script>
import PageHeader from "../componets/PageHeader";
import {adminPasswordUri} from "../../common/api";

export default {
  components: {
    PageHeader
  },
  data() {
    return {
      formData: {password: '', repeat_password: ''}
    }
  },
  methods: {
    onFormSubmit(values) {
      if(this.formData.password != this.formData.repeat_password) {
        this.$toast.fail('两次输入的密码不一致');
        return;
      }
      const that = this;
      that.$http.post(adminPasswordUri, that.formData, {emulateJSON: true}).then(response => {
        let {msg, code, data} = response.body
        if (code != 0) {
          that.$toast.fail(msg);
        } else {
          that.$toast.success('保存成功');
          that.$router.back();
        }
      }, response => {
        that.$toast.fail('发生错误');
      })
    },
  },
  mounted: function () {

  }
}
</script>

<style scoped>

</style>
